package com.chaoyi.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.Home;
import com.chaoyi.service.WhiteHomeService;
/**
 * 白包首页内容
 * @author wbf
 */
@Controller
@MultipartConfig
@RequestMapping("/whiteHome")
public class WhiteHomeController {
	@Resource
	WhiteHomeService whiteHomeService;
	/**
	 * 初始化跳转页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mod =new ModelAndView("/whiteHome/whiteHomeList");
		return mod;
	}
	/**
	 * 首页展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("homeList")
	@ResponseBody
	public JSONObject homeList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		String type = request.getParameter("type");
		List<Home> list = whiteHomeService.findList(offset, limit,type);
		int count =whiteHomeService.findCount(type);
		json.put("rows", list);
		json.put("total", count);
		return json;
	}
	
	/**
	 *  新增首页内容跳转页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("addHome_jump")
	public ModelAndView addHome_jump(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mod = new ModelAndView("/whiteHome/addWhiteHome");
		return mod;
	}
	/**
	 * 保存新增首页内容
	 */
	@ResponseBody
	@RequestMapping("addHome")
	public JSONObject addHome(HttpServletRequest request, HttpServletResponse response, Home home) {
		JSONObject json = new JSONObject();
		int number = whiteHomeService.saveHome(home);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 修改首页内容
	 */
	@RequestMapping("/alertHome")
	public ModelAndView alertHome(HttpServletRequest request, HttpServletResponse response,int videoId) {
		ModelAndView mod = new ModelAndView("/whiteHome/alertWhiteHome");
		Home home = whiteHomeService.findHomeOne(videoId);
		mod.addObject("obj",home);
		return mod;
	}
	
	/**
	 * 保存修改的私播信息
	 */
	@ResponseBody
	@RequestMapping("alertHomeSave")
	public JSONObject alertHomeSave(HttpServletRequest request, HttpServletResponse response, Home home) {
		JSONObject json = new JSONObject();
		int number = whiteHomeService.alertHomeSave(home);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 删除首页内容
	 */
	@ResponseBody
	@RequestMapping("deleteHome")
	public JSONObject deleteHome(HttpServletRequest request, HttpServletResponse response, int videoId) {
		JSONObject json = new JSONObject();
		int number = whiteHomeService.deleteHome(videoId);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 修改参数
	 */
	@ResponseBody
	@RequestMapping("updateParameter")
	public JSONObject updateParameter(HttpServletRequest request, HttpServletResponse response, String parameter) {
		JSONObject json = new JSONObject();
		int number = whiteHomeService.updateParameter(parameter);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 查询参数
	 */
	@ResponseBody
	@RequestMapping("findParameter")
	public JSONObject findParameter(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String values = whiteHomeService.findParameter();
		if (!values.equals("") && !values.equals("null")) {
			json.put("missage", "success");
			json.put("values", values);
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	
}
